<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>书籍列表 - 校园二手书交易平台</title>
    <link rel="stylesheet" th:href="@{/static/css/bootstrap.min.css}">
    <link rel="stylesheet" th:href="@{/static/css/font-awesome.min.css}">
    <style>
        .book-card {
            height: 100%;
            transition: transform 0.3s;
            border: 1px solid #eee;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .book-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .book-cover {
            height: 200px;
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }
        .book-info {
            padding: 15px;
        }
        .book-title {
            font-size: 1.1rem;
            font-weight: bold;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .book-author {
            font-size: 0.9rem;
            color: #666;
        }
        .book-price {
            font-size: 1.2rem;
            color: #e74c3c;
            font-weight: bold;
        }
        .book-original-price {
            font-size: 0.9rem;
            color: #999;
            text-decoration: line-through;
        }
        .book-condition {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: rgba(0, 0, 0, 0.6);
            color: white;
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 0.8rem;
        }
        .filter-section {
            background-color: #f8f9fa;
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .filter-title {
            font-size: 1.1rem;
            font-weight: bold;
            margin-bottom: 15px;
        }
        .filter-item {
            margin-bottom: 10px;
        }
        .navbar-nav .nav-item {
            margin-right: 15px;
        }
        .footer {
            background-color: #343a40;
            color: white;
            padding: 30px 0;
            margin-top: 50px;
        }
        .pagination-container {
            display: flex;
            justify-content: center;
            margin-top: 30px;
        }
        .sort-dropdown {
            display: inline-block;
            margin-right: 15px;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" th:href="@{/}">校园二手书交易平台</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/}"><i class="fas fa-home"></i> 首页</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" th:href="@{/book/list}"><i class="fas fa-book"></i> 书籍列表</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/book/publish}"><i class="fas fa-plus"></i> 发布书籍</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/book/my-books}">我的书籍</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/order/my-orders}">我的订单</a>
                    </li>
                </ul>
                <form class="form-inline my-2 my-lg-0 mr-3" th:action="@{/books/list}" method="get">
                    <div class="input-group">
                        <input class="form-control" type="search" placeholder="搜索书名..." name="keyword" 
                               th:value="${keywordFilter != null ? keywordFilter : ''}">
                        <div class="input-group-append">
                            <button class="btn btn-outline-light" type="submit">
                                <i class="fas fa-search"></i>
                            </button>
                        </div>
                    </div>
                </form>
                <ul class="navbar-nav">
                    <li class="nav-item dropdown" sec:authorize="isAuthenticated()">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown">
                            <span sec:authentication="name"></span>
                        </a>
                        <div class="dropdown-menu dropdown-menu-right">
                            <a class="dropdown-item" th:href="@{/user/profile}">个人信息</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" th:href="@{/logout}">退出登录</a>
                        </div>
                    </li>
                    <li class="nav-item" sec:authorize="!isAuthenticated()">
                        <a class="nav-link" th:href="@{/login}">登录</a>
                    </li>
                    <li class="nav-item" sec:authorize="!isAuthenticated()">
                        <a class="nav-link" th:href="@{/register}">注册</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 页面内容 -->
    <div class="container py-5">
        <div class="row">
            <!-- 左侧筛选栏 -->
            <div class="col-md-3">
                <div class="filter-section">
                    <h4 class="filter-title">筛选条件</h4>
                    <form th:action="@{/books/list}" method="get" id="filterForm">
                        <!-- 隐藏的排序字段 -->
                        <input type="hidden" name="sortBy" th:value="${sortBy}">
                        <input type="hidden" name="sortDirection" th:value="${sortDirection}">
                        
                        <!-- 分类筛选 -->
                        <div class="filter-item">
                            <h5>图书分类</h5>
                            <div class="form-check" th:each="cat : ${categories}">
                                <input class="form-check-input filter-checkbox" type="radio" name="category"
                                       th:id="'category-' + ${cat}" th:value="${cat}"
                                       th:checked="${categoryFilter != null && categoryFilter.equals(cat)}">
                                <label class="form-check-label" th:for="'category-' + ${cat}" th:text="${cat}"></label>
                            </div>
                        </div>
                        
                        <!-- 价格区间筛选 -->
                        <div class="filter-item">
                            <h5>价格区间</h5>
                            <div class="form-check" th:each="range : ${priceRanges}">
                                <input class="form-check-input filter-checkbox" type="radio" name="priceRange"
                                       th:id="'price-' + ${rangeStat.index}" 
                                       th:value="${range.min + '-' + (range.max != null ? range.max : '')}"
                                       th:checked="${minPriceFilter != null && minPriceFilter.equals(range.min) && 
                                                   (maxPriceFilter != null ? maxPriceFilter.equals(range.max) : range.max == null)}">
                                <label class="form-check-label" th:for="'price-' + ${rangeStat.index}" th:text="${range.display}"></label>
                            </div>
                        </div>
                        
                        <!-- 成色筛选 -->
                        <div class="filter-item">
                            <h5>书籍成色</h5>
                            <div class="form-check" th:each="cond : ${conditions}">
                                <input class="form-check-input filter-checkbox" type="radio" name="condition"
                                       th:id="'condition-' + ${cond.value}" th:value="${cond.value}"
                                       th:checked="${conditionFilter != null && conditionFilter == cond.value}">
                                <label class="form-check-label" th:for="'condition-' + ${cond.value}" th:text="${cond.display}"></label>
                            </div>
                        </div>
                        
                        <!-- 筛选按钮 -->
                        <div class="filter-item mt-4">
                            <button type="submit" class="btn btn-primary btn-block">应用筛选</button>
                            <a href="javascript:void(0)" class="btn btn-secondary btn-block mt-2" onclick="resetFilters()">重置筛选</a>
                        </div>
                    </form>
                </div>
            </div>
            
            <!-- 右侧书籍列表 -->
            <div class="col-md-9">
                <!-- 排序和结果数量 -->
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <div>
                        <span class="text-muted">共找到 <strong th:text="${totalItems}">0</strong> 本书籍</span>
                    </div>
                    <div class="d-flex">
                        <div class="sort-dropdown">
                            <label class="mb-0 mr-2">排序方式：</label>
                            <div class="dropdown d-inline-block">
                                <button class="btn btn-outline-secondary dropdown-toggle" type="button" id="sortDropdown" data-toggle="dropdown">
                                    <span th:if="${sortBy == 'createTime' && sortDirection == 'desc'}">最新发布</span>
                                    <span th:if="${sortBy == 'createTime' && sortDirection == 'asc'}">最早发布</span>
                                    <span th:if="${sortBy == 'price' && sortDirection == 'asc'}">价格从低到高</span>
                                    <span th:if="${sortBy == 'price' && sortDirection == 'desc'}">价格从高到低</span>
                                    <span th:if="${sortBy == 'views' && sortDirection == 'desc'}">热门程度</span>
                                </button>
                                <div class="dropdown-menu dropdown-menu-right">
                                    <a class="dropdown-item" href="javascript:void(0)" onclick="changeSort('createTime', 'desc')">最新发布</a>
                                    <a class="dropdown-item" href="javascript:void(0)" onclick="changeSort('createTime', 'asc')">最早发布</a>
                                    <a class="dropdown-item" href="javascript:void(0)" onclick="changeSort('price', 'asc')">价格从低到高</a>
                                    <a class="dropdown-item" href="javascript:void(0)" onclick="changeSort('price', 'desc')">价格从高到低</a>
                                    <a class="dropdown-item" href="javascript:void(0)" onclick="changeSort('views', 'desc')">热门程度</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 书籍卡片列表 -->
                <div class="row">
                    <div class="col-md-4 mb-4" th:each="book : ${books}">
                        <div class="card book-card">
                            <div class="book-condition" th:text="${book.condition == 1 ? '全新' : 
                                                                  book.condition == 2 ? '九成新' : 
                                                                  book.condition == 3 ? '八成新' : 
                                                                  book.condition == 4 ? '七成新' : '六成新及以下'}">
                            </div>
                            <div class="book-cover" th:style="'background-image: url(' + (${book.imageUrl != null && !book.imageUrl.isEmpty()} ? ${book.imageUrl} : '/static/img/default-book.jpg') + ')'"></div>
                            <div class="book-info">
                                <h5 class="book-title" th:text="${book.title}">书名</h5>
                                <p class="book-author" th:text="${book.author != null ? book.author : '未知作者'}">作者</p>
                                <div class="d-flex justify-content-between align-items-center">
                                    <div>
                                        <span class="book-price" th:text="'¥' + ${#numbers.formatDecimal(book.price, 1, 2)}">价格</span>
                                        <span class="book-original-price ml-2" th:if="${book.originalPrice != null}" 
                                              th:text="'¥' + ${#numbers.formatDecimal(book.originalPrice, 1, 2)}">原价</span>
                                    </div>
                                    <a th:href="@{'/book/detail/' + ${book.id}}" class="btn btn-sm btn-outline-primary">查看详情</a>
                                </div>
                                <p class="text-muted mt-2 mb-0 small">
                                    <span th:text="'卖家: ' + ${book.sellerName}">卖家</span>
                                    <span class="ml-2" th:text="'浏览: ' + ${book.views}">浏览量</span>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 无结果提示 -->
                <div class="alert alert-info" th:if="${books.isEmpty()}">
                    <i class="fas fa-info-circle"></i> 没有找到符合条件的书籍，请尝试调整筛选条件。
                </div>
                
                <!-- 分页 -->
                <div class="pagination-container" th:if="${totalPages > 1}">
                    <nav>
                        <ul class="pagination">
                            <li class="page-item" th:classappend="${currentPage == 1 ? 'disabled' : ''}">
                                <a class="page-link" href="javascript:void(0)" onclick="changePage(1)">首页</a>
                            </li>
                            <li class="page-item" th:classappend="${currentPage == 1 ? 'disabled' : ''}">
                                <a class="page-link" href="javascript:void(0)" th:onclick="'changePage(' + ${currentPage - 1} + ')'">上一页</a>
                            </li>
                            <li class="page-item" th:each="i : ${#numbers.sequence(1, totalPages)}" 
                                th:if="${i >= currentPage - 2 && i <= currentPage + 2}"
                                th:classappend="${i == currentPage ? 'active' : ''}">
                                <a class="page-link" href="javascript:void(0)" th:onclick="'changePage(' + ${i} + ')'" th:text="${i}">1</a>
                            </li>
                            <li class="page-item" th:classappend="${currentPage == totalPages ? 'disabled' : ''}">
                                <a class="page-link" href="javascript:void(0)" th:onclick="'changePage(' + ${currentPage + 1} + ')'">下一页</a>
                            </li>
                            <li class="page-item" th:classappend="${currentPage == totalPages ? 'disabled' : ''}">
                                <a class="page-link" href="javascript:void(0)" th:onclick="'changePage(' + ${totalPages} + ')'">末页</a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer th:replace="fragments/footer :: footer"></footer>

    <!-- JavaScript引用 -->
    <script th:src="@{/static/js/jquery.min.js}"></script>
    <script th:src="@{/static/js/bootstrap.bundle.min.js}"></script>
    
    <script th:inline="javascript">
        /*<![CDATA[*/
        // 切换排序方式
        function changeSort(sortBy, sortDirection) {
            $('input[name="sortBy"]').val(sortBy);
            $('input[name="sortDirection"]').val(sortDirection);
            $('#filterForm').submit();
        }
        
        // 切换页码
        function changePage(page) {
            // 创建一个新的表单
            var form = $('<form>');
            form.attr('method', 'get');
            form.attr('action', /*[[@{/books/list}]]*/ '');
            
            // 复制当前筛选条件
            var currentForm = $('#filterForm');
            currentForm.find('input[type="hidden"], input[type="radio"]:checked').each(function() {
                var input = $('<input>');
                input.attr('type', 'hidden');
                input.attr('name', $(this).attr('name'));
                input.attr('value', $(this).val());
                form.append(input);
            });
            
            // 添加页码
            var pageInput = $('<input>');
            pageInput.attr('type', 'hidden');
            pageInput.attr('name', 'page');
            pageInput.attr('value', page);
            form.append(pageInput);
            
            // 提交表单
            $('body').append(form);
            form.submit();
        }
        
        // 重置筛选条件
        function resetFilters() {
            window.location.href = /*[[@{/books/list}]]*/ '';
        }
        
        // 解析价格区间
        $(document).ready(function() {
            $('input[name="priceRange"]').change(function() {
                var range = $(this).val().split('-');
                var minPrice = range[0];
                var maxPrice = range[1];
                
                // 创建隐藏的价格字段
                if (minPrice) {
                    var minInput = $('<input>');
                    minInput.attr('type', 'hidden');
                    minInput.attr('name', 'minPrice');
                    minInput.attr('value', minPrice);
                    $('#filterForm').append(minInput);
                }
                
                if (maxPrice) {
                    var maxInput = $('<input>');
                    maxInput.attr('type', 'hidden');
                    maxInput.attr('name', 'maxPrice');
                    maxInput.attr('value', maxPrice);
                    $('#filterForm').append(maxInput);
                }
            });
        });
        /*]]>*/
    </script>
</body>
</html> 